<?php

namespace plugins\user\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\CommonUntil;
use plugins\user\model\UserModel;
use think\Db;

/**
 * 部门
 */
class AdminIndexController extends PluginAdminBaseController
{

    /**
     * 部门列表
     */
    public function index()
    {
        return $this->fetch();
    }

    public function add_page()
    {
        $param = $this->request->param();
        $this->assign('cri_code', $param['cri_code']);
        return $this->fetch();
    }

    public function edit_page()
    {
        $param = $this->request->param();
        $this->assign('id', $param['id']);
        return $this->fetch();
    }


    /**
     * 人员分页列表
     */
    public function getList()
    {
        $param = $this->request->param();
        $userModel = new UserModel();
        $pageSize = '10';
        if (!empty($param['pageSize'])) {
            $pageSize = $param['pageSize'];
        }
        $where['user_status'] = 1;
        $where['user_type'] = 2;
        if (!empty($param['user_login'])) {
            $where['user_login'] = ['like', '%' . $param['user_login'] . '%'];
        }
        $list = $userModel->where($where)->paginates($pageSize)->each(function ($item) use ($userModel) {
            return $userModel->getDetail($item);
        });
        return api_json_echo(true, '查询成功', $list);
    }

    /**
     * 获取部门详情
     */
    public function getUserDetail()
    {
        $param = $this->request->param();
        $userModel = new UserModel();
        if (empty($param['id'])) {
            return api_json_echo(false, '传参错误');
        }
        $info = $userModel->where('id', $param['id'])->find();
        return api_json_echo(true, '查询成功', $info);
    }


    /**
     * 添加人员
     */
    public function add()
    {
        $param = $this->request->param();
        $userModel = new UserModel();
        if (empty($param['department_id']) || empty($param['user_login']) || empty($param['user_pass']) || empty($param['d_pass'])) {
            return api_json_echo(false, '传参错误');
        }
        if (strlen($param['user_pass']) < 6 || strlen($param['user_pass']) > 16) {
            return api_json_echo(false, '密码长度不合法，长度应为6-16位！');
        }
        if (strlen($param['d_pass']) < 6 || strlen($param['d_pass']) > 16) {
            return api_json_echo(false, '确认密码长度不合法，长度应为6-16位！');
        }
        if ($param['user_pass'] !== $param['d_pass']) {
            return api_json_echo(false, '两次输入的密码不一致！');
        }
        if (is_array($param['address'])) {
            $address = end($param['address']);
            $param['address'] = $address;
        }
        $department_id = end($param['department_id']);
        $param['department_id'] = $department_id;
        //生成密码
        $param['user_pass'] = cmf_password($param['user_pass']);
        $param['create_uid'] = cmf_get_current_admin_id();
        $param['edit_time'] = time();
        $param['create_time'] = time();
        $param['user_nickname'] = $param['user_login'];
        $param['user_type'] = 2;
        $NO = $userModel->column('employee_id');
        $param['employee_id'] = max($NO) + 1;
        $re = $userModel->strict(false)->insert($param);
        if (empty($re)) {
            return api_json_echo(false, '提交失败');
        }
        return api_json_echo(true, '提交成功');
    }

    /**
     * 编辑人员
     */
    public function edit()
    {
        $param = $this->request->param();
        $userModel = new UserModel();
        if (empty($param['id']) || empty($param['department_id']) || empty($param['user_login']) || empty($param['user_pass']) || empty($param['d_pass'])) {
            return api_json_echo(false, '传参错误');
        }
        $user = $userModel->where('id', $param['id'])->find();
        if ($user['user_pass'] != $param['user_pass']) {
            if (strlen($param['user_pass']) < 6 || strlen($param['user_pass']) > 16) {
                return api_json_echo(false, '密码长度不合法，长度应为6-16位！');
            }
            if (strlen($param['d_pass']) < 6 || strlen($param['d_pass']) > 16) {
                return api_json_echo(false, '确认密码长度不合法，长度应为6-16位！');
            }
            if ($param['user_pass'] !== $param['d_pass']) {
                return api_json_echo(false, '两次输入的密码不一致！');
            }
            //生成密码
            $param['user_pass'] = cmf_password($param['user_pass']);
        }
        if (is_array($param['department_id'])) {
            $department_id = end($param['department_id']);
            $param['department_id'] = $department_id;
        }
        if (is_array($param['address'])) {
            $address = end($param['address']);
            $param['address'] = $address;
        }
        $param['edit_time'] = time();
        $param['user_nickname'] = $param['user_login'];
        $re = $userModel->strict(false)->where('id', $param['id'])->update($param);
        if ($re === null) {
            return api_json_echo(false, '提交失败');
        }
        return api_json_echo(true, '提交成功');
    }

    /**
     * 删除
     */
    public function del()
    {
        $param = $this->request->param();
        if (empty($param['id'])) {
            return api_json_echo(false, '传值错误');
        }
        $userModel = new UserModel();
        $id = $param['id'];
        $re = $userModel->where('id', $id)->delete();
        if (empty($re)) {
            return api_json_echo(false, '删除失败');
        }
        return api_json_echo(true, '删除成功');
    }

    /**
     * 上传用户照片
     */
    public function uploadUserAvatar()
    {
        $param = $this->request->param();
        $userModel = new UserModel();
        if (empty($param['id'])) {
            return api_json_echo(false, '未获取到ID');
        }
        $path = '/upload';
        if (empty($_FILES)) {
            return api_json_echo(false, '未获取到数据');
        }
        $rule = [
            'size' => 1024 * 1024 * 10,    //1024kb
            'ext' => ['jpeg', 'png', 'jpg'],
            'type' => ['image/jpeg', 'image/png', 'image/jpg']
        ];
        $res = uploadImg($_FILES ['file'], $path, $rule);
        if (!$res['status']) {
            return api_json_echo(false, $res ['message'], $res ['code']);
        }
        //更新系统内
        $edit_res = $userModel->where('id', $param['id'])->update(['avatar' => $res['data']]);
        if ($edit_res === null) {
            return api_json_echo(false, '修改失败');
        }
        return api_json_echo(true, '修改成功');
    }

    /**
     * 导出excel
     */
    public function exportExcel()
    {
        $commonUntil = new CommonUntil();

        $userModel = new UserModel();
        $where['user_status'] = 1;
        $where['user_type'] = 2;
        $list = $userModel->where($where)->select()->each(function ($item) use ($userModel) {
            return $userModel->getDetail($item);
        });
        $expDataList = [];
        foreach ($list as $item){
            $user_list = [];
            $user_list[] = $item['user_login'];
            $user_list[] = $item['mobile'];
            $user_list[] = $item['last_login_time'];
            $user_list[] = $item['job'];
            $user_list[] = $item['department_name'];
            $user_list[] = $item['address_name'];
            $user_list[] = $item['remark'];
            $expDataList[] = $user_list;
        }
        $expTitle = date('Y-m-d H:i').'-人员导出';
        $expHeadArr = ['姓名','手机号','最后登录时间','职位','部门','地区','备注'];
        $filePath = $commonUntil->exportExcelUntil($expTitle,$expHeadArr,$expDataList);
        return api_json_echo(true, '导出成功', zy_domain() . '/upload/' . $filePath);
    }
}
